


#include <dpl/config.h>
#if INCLUDE_SLEEP_TEST
#include <dpl/log.h>

D_LOG_DOMAIN(sleep_test,"this is a case, how to use");




static int simple_sleep_test(void)
{
    enum { COUNT = 10 };
    int i;
    dpl_status_t rc;
    
    dpl_i( "..will write messages every 1 second:");
    
    for (i=0; i<COUNT; ++i) {
	dpl_time_val tv;
	dpl_parsed_time pt;

	rc = dpl_thread_sleep(1000);
	if (rc != DPL_SUCCESS) {
	    dpl_e("...error: dpl_thread_sleep()", rc);
	    return -10;
	}

	rc = dpl_gettimeofday(&tv);
	if (rc != DPL_SUCCESS) {
	    dpl_e("...error: dpl_gettimeofday()", rc);
	    return -11;
	}

	dpl_time_decode(&tv, &pt);

	dpl_i("...%04d-%02d-%02d %02d:%02d:%02d.%03d",
		  pt.year, pt.mon, pt.day,
		  pt.hour, pt.min, pt.sec, pt.msec);

    }

    return 0;
}

static int sleep_duration_test(void)
{
    enum { MIS = 20};
    unsigned duration[] = { 2000, 1000, 500, 200, 100 };
    unsigned i;
    dpl_status_t rc;

    dpl_i( "..running sleep duration test");

    /* Test dpl_thread_sleep() and dpl_gettimeofday() */
    for (i=0; i<DPL_ARRAY_SIZE(duration); ++i) {
        dpl_time_val start, stop;
	dpl_uint32_t msec;

        /* Mark start of test. */
        rc = dpl_gettimeofday(&start);
        if (rc != DPL_SUCCESS) {
            dpl_e("...error: dpl_gettimeofday()", rc);
            return -10;
        }

        /* Sleep */
        rc = dpl_thread_sleep(duration[i]);
        if (rc != DPL_SUCCESS) {
            dpl_e("...error: dpl_thread_sleep()", rc);
            return -20;
        }

        /* Mark end of test. */
        rc = dpl_gettimeofday(&stop);

        /* Calculate duration (store in stop). */
        DPL_TIME_VAL_SUB(stop, start);

	/* Convert to msec. */
	msec = DPL_TIME_VAL_MSEC(stop);

	/* Check if it's within range. */
	if (msec < duration[i] * (100-MIS)/100 ||
	    msec > duration[i] * (100+MIS)/100)
	{
	    dpl_i( "...error: slept for %d ms instead of %d ms "
		      "(outside %d%% err window)",
		      msec, duration[i], MIS);
	    return -30;
	}
    }


    /* Test dpl_thread_sleep() and dpl_get_timestamp() and friends */
    for (i=0; i<DPL_ARRAY_SIZE(duration); ++i) {
	dpl_time_val t1, t2;
        dpl_timestamp start, stop;
	dpl_uint32_t msec;

	dpl_thread_sleep(0);

        /* Mark start of test. */
        rc = dpl_get_timestamp(&start);
        if (rc != DPL_SUCCESS) {
            dpl_e("...error: dpl_get_timestamp()", rc);
            return -60;
        }

	/* ..also with gettimeofday() */
	dpl_gettimeofday(&t1);

        /* Sleep */
        rc = dpl_thread_sleep(duration[i]);
        if (rc != DPL_SUCCESS) {
            dpl_e("...error: dpl_thread_sleep()", rc);
            return -70;
        }

        /* Mark end of test. */
        dpl_get_timestamp(&stop);

	/* ..also with gettimeofday() */
	dpl_gettimeofday(&t2);

	/* Compare t1 and t2. */
	if (DPL_TIME_VAL_LT(t2, t1)) {
	    dpl_i( "...error: t2 is less than t1!!");
	    return -75;
	}

        /* Get elapsed time in msec */
        msec = dpl_elapsed_msec(&start, &stop);

	/* Check if it's within range. */
	if (msec < duration[i] * (100-MIS)/100 ||
	    msec > duration[i] * (100+MIS)/100)
	{
	    dpl_i( "...error: slept for %d ms instead of %d ms "
		      "(outside %d%% err window)",
		      msec, duration[i], MIS);
	    DPL_TIME_VAL_SUB(t2, t1);
	    dpl_i( "...info: gettimeofday() reported duration is "
		      "%d msec",
		      DPL_TIME_VAL_MSEC(t2));

	    return -76;
	}
    }

    /* All done. */
    return 0;
}

int test_sleep()
{

	dpl_i("Enter test!");
	int rc;

	rc = simple_sleep_test();
	if (rc != DPL_SUCCESS)
		return rc;

	rc = sleep_duration_test();
	if (rc != DPL_SUCCESS)
		return rc;

	return 0;
}

#endif

